package com.example.healthmanagement.service;

import com.example.healthmanagement.dao.BmiRepository;
import com.example.healthmanagement.dao.CookBookRepository;
import com.example.healthmanagement.dao.FoodInfoRepository;
import com.example.healthmanagement.dao.FoodRemRepository;
import com.example.healthmanagement.entity.Bmi;
import com.example.healthmanagement.entity.CookBook;
import com.example.healthmanagement.entity.FoodInfo;
import com.example.healthmanagement.entity.FoodRem;
import com.example.healthmanagement.entity.vo.FoodRemVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class FoodRemService {

    private FoodRemRepository foodRemRepository;
    private FoodInfoRepository foodInfoRepository;
    private BmiRepository bmiRepository;
    private CookBookRepository cookBookRepository;

    @Autowired
    public FoodRemService(FoodRemRepository foodRemRepository, FoodInfoRepository foodInfoRepository, BmiRepository bmiRepository, CookBookRepository cookBookRepository) {
        this.foodRemRepository = foodRemRepository;
        this.foodInfoRepository = foodInfoRepository;
        this.bmiRepository = bmiRepository;
        this.cookBookRepository = cookBookRepository;
    }

    public boolean add(FoodRem foodRem) {
        if (foodRemRepository.save(foodRem) != null) {
            return true;
        } else {
            return false;
        }
    }

    public boolean update(FoodRem foodRem) {
        foodRemRepository.save(foodRem);
        return true;
    }

    public List<FoodRemVo> findAll(String name) {
        List<FoodRem> foodRems = new ArrayList<>();
        if (name != null) {
            foodRems = foodRemRepository.findFoodRemsByDeletedAndNameContaining(false, name);
        } else {
            foodRems = foodRemRepository.findFoodRemsByDeleted(false);
        }
        ArrayList<FoodRemVo> foodRemVos = new ArrayList<>();
        for (FoodRem foodRem : foodRems) {
            FoodRemVo foodRemVo = new FoodRemVo();
            foodRemVo.setId(foodRem.getId());
            foodRemVo.setName(foodRem.getName());
            foodRemVo.setFoodGuide(foodRem.getFoodGuide());
            foodRemVo.setCreatedAt(foodRem.getCreatedAt());
            foodRemVo.setUpdatedAt(foodRem.getUpdatedAt());
            foodRemVo.setFoodCatName(foodRem.getFoodCatName());
            Bmi bmi = bmiRepository.findBmiById(foodRem.getBmiLevel());
            foodRemVo.setBmiDesc(bmi.getDesc());
            foodRemVo.setBmiLevel(bmi.getId());
            CookBook cookBook = cookBookRepository.findCookBookByIdAndDeleted(foodRem.getCookBookId(), false);
            foodRemVo.setBookName(cookBook.getBookName());
            foodRemVo.setCookBookId(cookBook.getId());
            foodRemVos.add(foodRemVo);
        }
        return foodRemVos;
    }

    public FoodRem findById(int id) {
        return foodRemRepository.findFoodRemsById(id);
    }

    public FoodRemVo findRandomByCategory(String cateName) {
        List<FoodRem> foodRems = foodRemRepository.findFoodRemByFoodCatNameAndDeleted(cateName, false);
        if (foodRems == null || foodRems.isEmpty()) {
            return null;
        }
        Random rand = new Random();
        int randomIndex = rand.nextInt(foodRems.size());
        FoodRem foodRem = foodRems.get(randomIndex);
        CookBook cookBook = cookBookRepository.findCookBookByIdAndDeleted(foodRem.getCookBookId(), false);
        if (cookBook != null) {
            FoodRemVo foodRemVo = new FoodRemVo();
            foodRemVo.setId(foodRem.getId());
            foodRemVo.setName(foodRem.getName());
            foodRemVo.setBookContent(cookBook.getContent());
            return foodRemVo;
        }
        return null;
    }

    public FoodRemVo findRandomByBmi(double nowBmi) {
        Bmi bmi = bmiRepository.findBmiByValue(nowBmi);
        List<FoodRem> foodRems = foodRemRepository.findFoodRemByBmiLevelAndDeleted(bmi.getId(), false);
        Random rand = new Random();
        int randomIndex = rand.nextInt(foodRems.size());
        FoodRem foodRem = foodRems.get(randomIndex);
        if (foodRem != null) {
            CookBook cookBook = cookBookRepository.findCookBookByIdAndDeleted(foodRem.getCookBookId(), false);
            if (cookBook != null) {
                FoodRemVo foodRemVo = new FoodRemVo();
                foodRemVo.setId(foodRem.getId());
                foodRemVo.setName(foodRem.getName());
                foodRemVo.setFoodGuide(foodRem.getFoodGuide());
                foodRemVo.setBookContent(cookBook.getContent());
                return foodRemVo;
            }
        }
        return new FoodRemVo();
    }
}
